package com.myrock.zlbandy.gorock.HttpModel;

/**
 * Created by zlbandy on 2017/9/8.
 */
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PostHttp {
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient();

    String post(String url, String keyword) throws IOException {
        RequestBody requestBody = new FormBody.Builder()
                .add("name", keyword)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    String classifyPost(String url, String keyword) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("image",keyword);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(JSON,jsonObject.toString()))
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }


    public static void main(String[] args) throws IOException {
        PostHttp example = new PostHttp();
        JSONObject json = new JSONObject();
        String response = example.post("http://www.roundsapp.com/post", json.toString());
        System.out.println(response);
    }
}
